package com.sanshow.charging.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.TimeUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanshow.charging.MyApplication;
import com.sanshow.charging.R;
import com.sanshow.charging.adapter.Table1Adapter;
import com.sanshow.charging.db.Charging;
import com.sanshow.charging.db.ChargingDao;
import com.sanshow.charging.model.Table1Model;

import java.util.ArrayList;
import java.util.List;

public class Table1Fragment extends Fragment {

    RecyclerView rvTable;
    private Table1Adapter mTableAdapter;
    private ArrayList<Table1Model.ResultBean.ListBean> mTableList;

    public static Table1Fragment newInstance() {
        Table1Fragment fragment = new Table1Fragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_table1, container, false);
        initView(view);
        return view;
    }


    private void initView(View view) {

        refreshView(view);
        getData(2);

        ImageButton ibtRefresh = view.findViewById(R.id.btnRefresh1);
        ibtRefresh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getData(2);
            }
        });
    }

    /**
     * 刷新消息列表
     */
    private void refreshView(View view) {
        //1,加载空布局文件，便于第五步适配器在没有数据的时候加载
        View emptyView = View.inflate(getActivity(), R.layout.empty_view, null);
        //2，设置LayoutManager,LinearLayoutManager表示竖直向下
        rvTable = view.findViewById(R.id.rv_table1);
        rvTable.setLayoutManager(new LinearLayoutManager(getActivity()));
        //3，初始化一个无数据的适配器
        mTableAdapter = new Table1Adapter();
        //4，绑定recyclerView和适配器
        rvTable.setAdapter(mTableAdapter);
        //5，给recyclerView设置空布局
        mTableAdapter.setEmptyView(emptyView);
        //6，给recyclerView的每一个子列表添加点击事件
        /*mTableAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), "我点击了第" + position + "个子view",
                        Toast.LENGTH_SHORT).show();
            }
        });*/
        //7，添加头文件和脚文件
        View headView = getLayoutInflater().inflate(R.layout.item_table1_header, null);
        View footView = getLayoutInflater().inflate(R.layout.item_table_footer, null);
        mTableAdapter.addHeaderView(headView);
        mTableAdapter.addFooterView(footView);
    }


    private void getData(int mode) {
        //添加临时数据，一般直接从接口获取
        switch (mode) {
            case 2:
                ChargingDao chargingDao = MyApplication.getDaoInstant().getChargingDao();
                List<Charging> chargingList = chargingDao.queryBuilder().where(ChargingDao.Properties.ItemCode.lt(16)).list();
                mTableList = new ArrayList<>();
                for (Charging charging : chargingList) {
                    String chargingText = "未充电";
                    if (charging.getCharging() == 1) {
                        chargingText = "充电中";
                    }

                    String doorStatusText = "关";
                    if (charging.getDoorStatus().equals("01")) {
                        doorStatusText = "开";
                    }

                    String doorIsAvailableText = "可用";
                    if (charging.getIsAvailable() == 0) {
                        doorIsAvailableText = "不可用";
                    }

                    String updateTime = "";
                    if (charging.getUpdateTime() != null) {
                        updateTime = TimeUtils.date2String(charging.getUpdateTime(), "MM-dd HH:mm:ss");
                    }

                    mTableList.add(new Table1Model.ResultBean.ListBean(charging.getItemCode(), charging.getInfoCode(), chargingText, String.valueOf(charging.getDianLiang()), doorStatusText, doorIsAvailableText, updateTime));
                }
                //更新数据
                mTableAdapter.setNewData(mTableList);
                break;
        }
    }
}
